import http from '@/util/http'
import constant from '@/constant'
import {actionInterceptor as interceptor} from '../interceptor'

const {TOKEN, ROLE} = constant.user
const {LOGIN, LOGOUT, UPDATE_PSW, WORK_LIST, SUBBORDINATES, ALL_LIST, DELETE, TOGGLE, RESETPWD, UPDATE, INSERT, BASE_INFO} = constant.user

let actions = {
    async login ({commit}, params) {
        let resp = await http.post('/login', params)
        commit(LOGIN, {...resp.data, ...resp.meta})
        window.localStorage.setItem(TOKEN, resp.meta.token)
        window.localStorage.setItem(ROLE, resp.meta.role)
    },
    async logout ({commit}, params) {
        let resp = await http.post('/logout', params)
        commit(LOGOUT, resp.data)
        window.localStorage.removeItem(TOKEN)
        window.localStorage.removeItem(ROLE)
        window.localStorage.removeItem('ZONE')
    },
    async detailed ({commit}, params) {
        try {
            let resp = await http.get('/profile', params)
            commit(LOGIN, resp.data)
        } catch (e) {
            window.localStorage.removeItem(TOKEN)
            window.localStorage.removeItem(ROLE)
            window.localStorage.removeItem('ZONE')
            throw e
        }
    },
    async update_password ({commit}, params) {
        let resp = await http.post('/password', params)
        commit(UPDATE_PSW, resp.data)
    },
    async userWorkList ({commit}) {
        let resp = await http.get('/user/getworklist')
        commit(WORK_LIST, resp.data)
    },
    async getSubordinates ({commit}) {
        let resp = await http.get('/user/map')
        commit(SUBBORDINATES, resp.result)
    },
    async userAllList ({commit}, params) {
        let resp = await http.get('/user', {params})
        commit(ALL_LIST, resp)
    },
    async deleteUser ({commit}, params) {
        await http.delete(`/user/${params.id}`)
        commit(DELETE)
    },
    async updateUser ({commit}, params) {
        await http.put(`/user/${params.id}`, params.info)
        commit(UPDATE)
    },
    async addUser ({commit}, params) {
        await http.post(`/user`, params.info)
        commit(INSERT)
    },
    async toggleUser ({commit}, params) {
        await http.post(`/user/${params.id}/state`)
        commit(TOGGLE)
    },
    async resetPwd ({commit}, params) {
        await http.post(`/user/password`, params)
        commit(RESETPWD)
    },
    async getProjectBase ({commit}) {
        let resp = await http.get('/user/projectbase')
        commit(BASE_INFO, resp)
    },
    async setProjectBase ({commit}, params) {
        await http.post('/user/projectbase', params)
        commit(BASE_INFO, params)
    }
}

actions.detailed.prototype.__native = true
actions.login.prototype.async_reg_commit = LOGIN
actions.update_password.prototype.async_reg_commit = UPDATE_PSW
actions.userWorkList.prototype.async_reg_commit = WORK_LIST
actions.getSubordinates.prototype.async_reg_commit = SUBBORDINATES
actions.userAllList.prototype.async_reg_commit = ALL_LIST
actions.deleteUser.prototype.async_reg_commit = DELETE
actions.toggleUser.prototype.async_reg_commit = TOGGLE
actions.resetPwd.prototype.async_reg_commit = RESETPWD
actions.updateUser.prototype.async_reg_commit = UPDATE
actions.addUser.prototype.async_reg_commit = INSERT
actions.getProjectBase.prototype.async_reg_commit = BASE_INFO
actions.setProjectBase.prototype.async_reg_commit = BASE_INFO

export default interceptor(actions)
// export default actions
